# -*- coding:utf-8 -*-

import sys

import logging              #日志相关
from os import path         #目录相关
import os

import getNetIp
import mymail
import myconfig                         #配置类

ip_data_cfg = myconfig.ip_file			#读取ip更新写入文件的地址

def compare(newip):
	#fp = open('ip.cfg',mode='r')		
	try:
		fp = open(ip_data_cfg,mode='r')								
	except:									#打开失败
		fp = open(ip_data_cfg,mode='w+')
		fp.close()
		print('creat new cfg file:' + ip_data_cfg)
		fp = open(ip_data_cfg,mode='r')		

	
	oldip = fp.read()
	fp.close()
#	print("fp:",oldip)
	
	if oldip == newip:
#		print("==")
		return True
	else:
#		print("!=")
		#fp = open('ip.cfg',mode='w+')	#非接续写
		fp = open(ip_data_cfg,'w+')	#非接续写
		fp.write(newip)
		fp.close()
		return False
		#print(fp.read())

####################   main    #################################################
#argv = sys.argv[1]
print('hello Demo ')
#print('hello Demo argv:'+argv)
#if argv == '':
#	print('argv is empty')
#	return

mypath = os.path.abspath("test.log")

LOG_FORMAT = "%(asctime)s %(name)s %(levelname)s %(pathname)s %(message)s "	#配置输出日志格式
DATE_FORMAT = '%Y-%m-%d  %H:%M:%S %a ' 										#配置输出时间的格式，注意月份和天数不要搞乱了

try:
    logging.basicConfig(level=logging.DEBUG,
                        format=LOG_FORMAT,
                        datefmt = DATE_FORMAT ,
                        filename="/home/share/samba/mymailip/test.log"				#这里一定要手动输入`pwd`命令执行后的绝对路径，使用相对路径将会导致crontab无法输出日志
                        #filename=r"d:\test\test.log" #有了filename参数就不会直接输出显示到控制台，而是直接写入文件
                        )
except:
	print("请检查路径日志路径是否正确\n")


print("path:"+ str(mypath))

wlan_ip = getNetIp.get_dev_ip()		#通过网站获取当前外网端口ip
logging.info("task get ip")

ret = False							#compare(wlan_ip)
if ret != True:						#IP更新了，实时操作
	print('refresh ip')
	logging.info("task refresh ip")
	mymail.send_IPAddr_mail(wlan_ip)
					

